package com.guodu.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.MaterialKcjgDao;
import com.guodu.entity.MaterialKcjg;
import com.guodu.entity.MaterialkcjgStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.vo.MaterialKcjgQueryVo;


@Repository("materialKcjgDao")
public class MaterialKcjgDaoImpl extends BaseDao implements MaterialKcjgDao {


	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialKcjg> findByVo(MaterialKcjgQueryVo mkqv, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialKcjg m where 1=1");
		if(mkqv!=null){
			if (mkqv.getFunitsId()!=null) {
				hql.append("    and exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+mkqv.getFunitsId()+" )");
			}
			if (mkqv.getUnitsId()!=null) {
				hql.append("    and exists ( select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+mkqv.getUnitsId()+")");
			}
			if (mkqv.getYears()!=null && !"".equals(mkqv.getYears())) {
				hql.append("    and TO_DATE(years,'yyyy-mm') = TO_DATE('"+mkqv.getYears()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2)");
		}
		hql.append(" order by jgId desc");
		return super.find(hql.toString());
	}

	@Override
	public void changeStatus(List<MaterialkcjgStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialKcjg set status =");
		hql.append(status);
		hql.append(" where jgId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialkcjgStatus> findStatus(String jgId) {
		// TODO Auto-generated method stub
		return super.findByProperty(MaterialkcjgStatus.class, "jgId", jgId);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		super.deleteAllById(MaterialKcjg.class, delIds);
	}

	@Override
	public void update(MaterialKcjg materialKcjg) {
		// TODO Auto-generated method stub
		super.update(materialKcjg);
	}

	@Override
	public void save(MaterialKcjg materialKcjg) {
		// TODO Auto-generated method stub
		super.save(materialKcjg);
	}

	@Override
	public MaterialKcjg findById(String jgId) {
		// TODO Auto-generated method stub
		return (MaterialKcjg) super.findById(MaterialKcjg.class, jgId);
	}

	@Override
	public List<?> ajax(String nian, UserInfo user) {
		// TODO Auto-generated method stub
		String hql = "from MaterialKcjg m where m.years = '"+nian+"' and createPeople = '"+user.getUserId()+"'";
		return super.find(hql.toString());
	}

}
